package.path = "./plugin/TQS601/utils.lua"..";..\\?.lua"
package.path = "./plugin/TQS601/json.lua"..";..\\?.lua"

require "utils.lua"
require "json.lua"

rxBuf = {}
regAddr = 0

function NewVariables()
    -- body
    local VariableMap = {
        Variable = {
        },
    }

    return VariableMap
end

function GenerateGetRealVariables(sAddr,step)

    -- body
    local crc
    local requestADU = {}
    local VariableMap = {
        Status = "0",
        Variable = {},
    }

    if(step == 0)
    then
        ----------读运行参数-----------------
        RegAddr = 22
        RegCnt = 4
        requestADU[1] = sAddr
        requestADU[2] = 0x03
        requestADU[3] = math.modf(RegAddr/256)
        requestADU[4] = math.modf(RegAddr%256)
        requestADU[5] = math.modf(RegCnt/256)
        requestADU[6] = math.modf(RegCnt%256)
        local VariableMapTemp = {
            Variable = requestADU
        }
        crc = GetCRCModbus(VariableMapTemp)
        requestADU[7] = math.modf(crc%256)
        requestADU[8] = math.modf(crc/256)

        regAddr = 40022
        VariableMap = {
            Status = "1",
            Variable = requestADU
        }
    elseif(step == 1)
    then
        ----------读运行参数-----------------
        RegAddr = 0
        RegCnt = 2
        requestADU[1] = sAddr
        requestADU[2] = 0x03
        requestADU[3] = math.modf(RegAddr/256)
        requestADU[4] = math.modf(RegAddr%256)
        requestADU[5] = math.modf(RegCnt/256)
        requestADU[6] = math.modf(RegCnt%256)
        local VariableMapTemp = {
            Variable = requestADU
        }
        crc = GetCRCModbus(VariableMapTemp)
        requestADU[7] = math.modf(crc%256)
        requestADU[8] = math.modf(crc/256)

        regAddr = 40001
        VariableMap = {
            Status = "1",
            Variable = requestADU
        }
    elseif(step==2)
    then
        ----------读开关量-----------------
        RegAddr = 0
        RegCnt = 6
        requestADU[1] = sAddr
        requestADU[2] = 0x02
        requestADU[3] = math.modf(RegAddr/256)
        requestADU[4] = math.modf(RegAddr%256)
        requestADU[5] = math.modf(RegCnt/256)
        requestADU[6] = math.modf(RegCnt%256)
        local VariableMapTemp = {
            Variable = requestADU
        }
        crc = GetCRCModbus(VariableMapTemp)
        requestADU[7] = math.modf(crc%256)
        requestADU[8] = math.modf(crc/256)

        regAddr = 10001
        VariableMap = {
            Status = "0",
            Variable = requestADU
        }
    end


    return VariableMap
end

function DeviceCustomCmd(sAddr,cmdName,cmdParam,step)

    -- body
    local crc
    local requestADU = {}
    local VariableMap = {
        Status = "0",
        Variable = {},
    }
    local temp = 0

    if(cmdName == "GetRealVariables")
    then
        if(step == 0)
        then
            RegAddr = 22
            RegCnt = 4
            requestADU[1] = sAddr
            requestADU[2] = 0x03
            requestADU[3] = math.modf(RegAddr/256)
            requestADU[4] = math.modf(RegAddr%256)
            requestADU[5] = math.modf(RegCnt/256)
            requestADU[6] = math.modf(RegCnt%256)
            local VariableMapTemp = {
                Variable = requestADU
            }
            crc = GetCRCModbus(VariableMapTemp)
            requestADU[7] = math.modf(crc%256)
            requestADU[8] = math.modf(crc/256)

            regAddr = 40022
            VariableMap = {
                Status = "1",
                Variable = requestADU
            }
        elseif(step==1)
        then
            RegAddr = 0
            RegCnt = 2
            requestADU[1] = sAddr
            requestADU[2] = 0x03
            requestADU[3] = math.modf(RegAddr/256)
            requestADU[4] = math.modf(RegAddr%256)
            requestADU[5] = math.modf(RegCnt/256)
            requestADU[6] = math.modf(RegCnt%256)
            local VariableMapTemp = {
                Variable = requestADU
            }
            crc = GetCRCModbus(VariableMapTemp)
            requestADU[7] = math.modf(crc%256)
            requestADU[8] = math.modf(crc/256)
    
            regAddr = 40001
            VariableMap = {
                Status = "1",
                Variable = requestADU
            }
        elseif(step==2)
        then
            RegAddr = 0
            RegCnt = 6
            requestADU[1] = sAddr
            requestADU[2] = 0x02
            requestADU[3] = math.modf(RegAddr/256)
            requestADU[4] = math.modf(RegAddr%256)
            requestADU[5] = math.modf(RegCnt/256)
            requestADU[6] = math.modf(RegCnt%256)
            local VariableMapTemp = {
                Variable = requestADU
            }
            crc = GetCRCModbus(VariableMapTemp)
            requestADU[7] = math.modf(crc%256)
            requestADU[8] = math.modf(crc/256)
    
            regAddr = 10001
            VariableMap = {
                Status = "0",
                Variable = requestADU
            }
        end
    elseif(cmdName == "SetVariables")
    then
        res = json.jsondecode(cmdParam)
        if (res["TempUpper"] ~= nil)
        then
            requestADU[1] = sAddr
            requestADU[2] = 0x06
            requestADU[3] = 0x00
            requestADU[4] = 0x16
            TempUpper = tonumber(res["TempUpper"])
            requestADU[5] = math.modf(TempUpper/256)
            requestADU[6] = math.modf(TempUpper%256)
            local VariableMapTemp = {
                Variable = requestADU
            }
            crc = GetCRCModbus(VariableMapTemp)
            requestADU[7] = math.modf(crc%256)
            requestADU[8] = math.modf(crc/256)

            regAddr = 0x0016
            VariableMap = {
                Status = "0",
                Variable = requestADU
            }
        elseif (res["TempLower"] ~= nil)
        then
            requestADU[1] = sAddr
            requestADU[2] = 0x06
            requestADU[3] = 0x00
            requestADU[4] = 0x17
            TempUpper = tonumber(res["TempLower"])
            print(TempUpper)
            requestADU[5] = math.modf(TempUpper/256)
            requestADU[6] = math.modf(TempUpper%256)
            local VariableMapTemp = {
                Variable = requestADU
            }
            crc = GetCRCModbus(VariableMapTemp)
            requestADU[7] = math.modf(crc%256)
            requestADU[8] = math.modf(crc/256)

            regAddr = 0x0017
            VariableMap = {
                Status = "0",
                Variable = requestADU
            }
        elseif (res["HumiUpper"] ~= nil)
        then
            requestADU[1] = sAddr
            requestADU[2] = 0x06
            requestADU[3] = 0x00
            requestADU[4] = 0x18
            TempUpper = tonumber(res["HumiUpper"])
            requestADU[5] = math.modf(TempUpper/256)
            requestADU[6] = math.modf(TempUpper%256)
            local VariableMapTemp = {
                Variable = requestADU
            }
            crc = GetCRCModbus(VariableMapTemp)
            requestADU[7] = math.modf(crc%256)
            requestADU[8] = math.modf(crc/256)

            regAddr = 0x0018
            VariableMap = {
                Status = "0",
                Variable = requestADU
            }
        elseif (res["HumiLower"] ~= nil)
        then
            requestADU[1] = sAddr
            requestADU[2] = 0x06
            requestADU[3] = 0x00
            requestADU[4] = 0x19
            TempUpper = tonumber(res["HumiLower"])
            requestADU[5] = math.modf(TempUpper/256)
            requestADU[6] = math.modf(TempUpper%256)
            local VariableMapTemp = {
                Variable = requestADU
            }
            crc = GetCRCModbus(VariableMapTemp)
            requestADU[7] = math.modf(crc%256)
            requestADU[8] = math.modf(crc/256)

            regAddr = 0x0019
            VariableMap = {
                Status = "0",
                Variable = requestADU
            }
        end
    end

    return VariableMap
end

function AnalysisRx(sAddr,rxBufCnt)

    local index = 1
    local mAddr = 0
    local mFunCode = 0
    local mRegByte = 0
    local crcH,crcL,crc,crcTemp

    -- body
    local VariableMap = {
        Status = "1",
        Variable = {},
    }

    --modbus03 06 最短为8个字节
    if(rxBufCnt < 6)
    then
        rxBuf = {}
        return VariableMap
    end


    for index=1,rxBufCnt,1 do
        if(index <= rxBufCnt)
        then
            mAddr     = rxBuf[index]
            mFunCode  = rxBuf[index+1]
            if (mAddr == tonumber(sAddr))
            then
                if(mFunCode == 0x03)
                then
                    mRegByte = rxBuf[index+2]
                    if(mRegByte+5 > rxBufCnt)
                    then
                        rxBuf = {}
                        return VariableMap
                    end
                    --取出数据域数据
                    local pdu = {}
                    for i=index,index+2+mRegByte,1 do
                        if rxBuf[i] ~= nil then
                            table.insert(pdu, rxBuf[i])
                        end
                    end
                    local VariableMapCRC = {
                        Variable = pdu
                    }
                    crc = GetCRCModbus(VariableMapCRC)
                    crcTemp = rxBuf[index+3+mRegByte+1]*256+rxBuf[index+3+mRegByte]
                    if(crc == crcTemp)
                    then
                        local Variable = {}
                        local varIndex = 3
                        local VariableIndex = 1
                        if(regAddr == 40022)
                        then
                            ---------------------------温度上限----------------------------------
                            local TempUpper = rxBuf[index+varIndex]*256+rxBuf[index+varIndex+1]
                            varIndex = varIndex + 2
                            Variable[VariableIndex] = utils.AppendVariable(VariableIndex,"TempUpper",TempUpper,TempUpper)
                            VariableIndex = VariableIndex + 1
                            ---------------------------温度下限----------------------------------
                            local TempLower = rxBuf[index+varIndex]*256+rxBuf[index+varIndex+1]
                            TempLower = utils.Int16Complement(TempLower)
                            varIndex = varIndex + 2
                            Variable[VariableIndex] = utils.AppendVariable(VariableIndex,"TempLower",TempLower,TempLower)
                            VariableIndex = VariableIndex + 1

                            ---------------------------湿度度上限----------------------------------
                            local HumiUpper = rxBuf[index+varIndex]*256+rxBuf[index+varIndex+1]
                            varIndex = varIndex + 2
                            Variable[VariableIndex] = utils.AppendVariable(VariableIndex,"HumiUpper",HumiUpper,HumiUpper)
                            VariableIndex = VariableIndex + 1
                            ---------------------------湿度下限----------------------------------
                            local HumiLower = rxBuf[index+varIndex]*256+rxBuf[index+varIndex+1]
                            varIndex = varIndex + 2
                            Variable[VariableIndex] = utils.AppendVariable(VariableIndex,"HumiLower",HumiLower,HumiLower)
                            VariableIndex = VariableIndex + 1

                            VariableMap = {
                                Status = "0",
                                Variable = Variable
                            }
                        elseif (regAddr == 40001)
                        then
                            local temp = rxBuf[index+varIndex]*256+rxBuf[index+varIndex+1]
                            varIndex = varIndex + 2
                            Variable[VariableIndex] = utils.AppendVariable(VariableIndex,"temp",temp,temp)
                            VariableIndex = VariableIndex + 1
                            ---------------------------湿度----------------------------------
                            local humi = rxBuf[index+varIndex]*256+rxBuf[index+varIndex+1]
                            varIndex = varIndex + 2
                            Variable[VariableIndex] = utils.AppendVariable(VariableIndex,"humi",humi,humi)
                            VariableIndex = VariableIndex + 1

                            VariableMap = {
                                Status = "0",
                                Variable = Variable
                            }
                        end
                        rxBuf = {}
                        return VariableMap
                    end
                elseif(mFunCode == 0x01 or mFunCode == 0x02)
                    then
                        mRegByte = rxBuf[index+2]
                        if(mRegByte+5 > rxBufCnt)
                        then
                            rxBuf = {}
                            return VariableMap
                        end
                        --取出数据域数据
                        local pdu = {}
                        for i=index,index+2+mRegByte,1 do
                            if rxBuf[i] ~= nil then
                                table.insert(pdu, rxBuf[i])
                            end
                        end
                        local VariableMapCRC = {
                            Variable = pdu
                        }
                        crc = GetCRCModbus(VariableMapCRC)
                        crcTemp = rxBuf[index+3+mRegByte+1]*256+rxBuf[index+3+mRegByte]
                        if(crc == crcTemp)
                        then
                            local Variable = {}
                            local varIndex = 3
                            local VariableIndex = 1
                            if(regAddr == 10001)
                            then
                                ---------------------------温度----------------------------------
                                local relay = rxBuf[index+varIndex]
                                varIndex = varIndex + 1

                                Variable[VariableIndex] = UpdateAlarm(relay,0x0001,VariableIndex,"IN1")
                                VariableIndex = VariableIndex + 1

                                Variable[VariableIndex] = UpdateAlarm(relay,0x0002,VariableIndex,"IN2")
                                VariableIndex = VariableIndex + 1

                                Variable[VariableIndex] = UpdateAlarm(relay,0x0004,VariableIndex,"Water1")
                                VariableIndex = VariableIndex + 1

                                Variable[VariableIndex] = UpdateAlarm(relay,0x0008,VariableIndex,"Water2")
                                VariableIndex = VariableIndex + 1
                                -------------------------------------------------------------
                                VariableMap = {
                                    Status = "0",
                                    Variable = Variable
                                }
                            end
                            rxBuf = {}
                            return VariableMap
                        end
                elseif(mFunCode == 0x05)
                then
                    mRegAddr = rxBuf[index+2]*256+rxBuf[index+3]
                    --取出数据域数据
                    local pdu = {}
                    for i=index,index+5,1 do
                        if rxBuf[i] ~= nil then
                            table.insert(pdu, rxBuf[i])
                        end
                    end
                    local VariableMapCRC = {
                        Variable = pdu
                    }
                    crc = GetCRCModbus(VariableMapCRC)
                    crcTemp = rxBuf[index+6+1]*256+rxBuf[index+6]
                    if(crc == crcTemp)
                    then
                        VariableMap = {
                            Status = "0",
                            Variable = Variable
                        }
                        rxBuf = {}
                        return VariableMap
                    end
                elseif(mFunCode == 0x06)
                then
                    mRegAddr = rxBuf[index+2]*256+rxBuf[index+3]
                    --取出数据域数据
                    local pdu = {}
                    for i=index,index+5,1 do
                        if rxBuf[i] ~= nil then
                            table.insert(pdu, rxBuf[i])
                        end
                    end
                    local VariableMapCRC = {
                        Variable = pdu
                    }
                    crc = GetCRCModbus(VariableMapCRC)
                    crcTemp = rxBuf[index+6+1]*256+rxBuf[index+6]
                    if(crc == crcTemp)
                    then
                        VariableMap = {
                            Status = "0",
                            Variable = Variable
                        }
                        rxBuf = {}
                        return VariableMap
                    end    
                else
                    index = index + 1
                end
            else
                index = index + 1
            end
        else
            rxBuf = {}
            return VariableMap
        end
    end

    rxBuf = {}
    return VariableMap
end

function UpdateAlarm(alarm,value,VariableIndex,name)
    local Variable

    if(utils.And(alarm,value) == 0x0000) then
        Variable = utils.AppendVariable(VariableIndex,name,0,"0")
    else
        Variable = utils.AppendVariable(VariableIndex,name,1,"1")
    end

    return Variable
end
